package com.touwowo.three;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.touwowo.utils.RabbitMqUtils;
import com.touwowo.utils.SleepUtils;

public class Worker {
    private static final String QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        //不公平分发，设置预取值为1
        channel.basicQos(2);
        System.out.println("工作线程 2 已经启动：");
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            //SleepUtils.sleep(30);
            System.out.println("收到的消息："+new String(message.getBody())+" consumerTag:"+consumerTag);
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
        };
        channel.basicConsume(QUEUE_NAME,false,deliverCallback,consumerTag -> {
            System.out.println("消费者消费取消。");
        });
    }
}
